package com.hanbang.gycatering.property;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.myviews.GridViewForScrollView;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyLog;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MyToast;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class FixDetail extends BaseActivity implements OnClickListener {
	private GridViewForScrollView gridview;
	private List<String> gridlist = new ArrayList<String>();
	private int fixRecordId,fixstate;
	private LinearLayout ll_waitfix;
	private TextView fixdetail_content,fix_time,fixreply_time;
	private WebView fixdetail_replay;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fixdetail);
		initView();
		getrepairsdetails();
	}
	
	private void initView() {
		// TODO Auto-generated method stub
		((TextView) findViewById(R.id.top_bar_title)).setText("报修详情");
		setBackArrows((ImageView) findViewById(R.id.top_bar_left_image));
		gridview = (GridViewForScrollView) findViewById(R.id.fix_gridview);
		fixdetail_content=(TextView) findViewById(R.id.fixdetail_content);
		fixdetail_replay=(WebView) findViewById(R.id.fixdetail_replay);
		fix_time=(TextView) findViewById(R.id.fix_time);
		fixreply_time=(TextView) findViewById(R.id.fixreply_time);
		ll_waitfix=(LinearLayout) findViewById(R.id.ll_waitfix);
		Intent intent=getIntent();
		fixRecordId=intent.getIntExtra("fixRecordId", 0);
		fixstate=intent.getIntExtra("fixstate", 0);
	}
	private void getrepairsdetails() {
		// TODO Auto-generated method stub
		((MyApplication) getApplicationContext()).httpUtils
		.send(HttpMethod.GET, HttpInterfaces.repairsdetails
				+ "&userphone=" + userData.getUserphone()
				+ "&password=" + userData.getUserpassword()
				+ "&RepairsId=" + fixRecordId,
				new MyRequestCallBack<String>(FixDetail.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(
							ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								JSONObject infor = json
										.getJSONObject("info");
								fixdetail_content.setText(infor
										.getString("lcontent"));
								MyLog.e("hehehehhehe", infor
										.getString("lcontent"));
								if(!"".equals(infor
										.getString("img1"))){
									gridlist.add(HttpInterfaces.ORG+infor
										.getString("img1"));
								}
									if(!"".equals(infor
											.getString("img2"))){
										gridlist.add(HttpInterfaces.ORG+infor
												.getString("img2"));
									}
										if(!"".equals(infor
												.getString("img3"))){
											gridlist.add(HttpInterfaces.ORG+infor
													.getString("img3"));
										}
										MyAdapter myadapter = new MyAdapter(FixDetail.this, gridlist);
										gridview.setAdapter(myadapter);
										gridview.setEnabled(false);	
										fix_time.setText(infor
										.getString("ltime"));
								if (fixstate==1) {
									ll_waitfix.setVisibility(View.VISIBLE);
									fixdetail_replay.loadDataWithBaseURL(null, infor
											.getString("rcontent"), "text/html",
											"utf-8", null); 
									fixreply_time.setText(infor
											.getString("rtime"));
								}
							} else {
								MyToast.show(FixDetail.this, "获取数据失败",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(FixDetail.this, "获取数据失败",
									Toast.LENGTH_SHORT);
						}
					}
				});
		 
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		 

		default:
			break;
		}
	}

	class MyAdapter extends BaseAdapter {
		// 上下文对象
		private Context context;
		// 图片数组
		private List<String> imgs = new ArrayList<String>();

		MyAdapter(Context context, List lists) {
			this.context = context;
			this.imgs = lists;
		}

		public int getCount() {
			return imgs.size();
		}

		public Object getItem(int item) {
			return item;
		}
		public long getItemId(int id) {
			return id;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(
						R.layout.item_gridview, null);
				holder.imageview = (ImageView) convertView.findViewById(R.id.item_fixgrid);
				holder.imageview.setVisibility(View.VISIBLE);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			bitmapUtils.display(holder.imageview,
					imgs.get(position));
			return convertView;

		}

		private class ViewHolder {
			ImageView imageview;
		}
	}
 
	 
}
	
	 